package com.nopay.cdxphone.ui.ativity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.cdxphone.base.BaseActivity;
import com.nopay.cdxphone.bean.CommonResponse;
import com.nopay.cdxphone.bean.request.ForgetPasswordRequest;
import com.nopay.cdxphone.bean.response.GetVerifiCodeResponse;
import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.ActivityForgetPwdBinding;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: java类作用描述
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ForgetActivity extends BaseActivity<ActivityForgetPwdBinding> {

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(ForgetActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(v -> mSwipeBackHelper.backward());

        binding.tvVerificationCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userPhone = binding.loginEtUser.getText().toString().trim();
                if (!TextUtils.isEmpty(userPhone)) {
                    if (RegexUtils.isMobileSimple(userPhone)) {
                        //请求验证码接口
                        doVerifyCode();
                        new CountDownTimer(60000, 1000) {

                            @Override
                            public void onTick(long millisUntilFinished) {
                                int second = (int) (Math.round((double) millisUntilFinished / 1000) - 1);
                                binding.tvVerificationCode.setClickable(false);
                                binding.tvVerificationCode.setText("剩余" + second + "秒");

                            }

                            @Override
                            public void onFinish() {
                                binding.tvVerificationCode.setClickable(true);
                                binding.tvVerificationCode.setText("获取验证码");
                            }
                        }.start();
                    } else {
                        ToastUtils.showShort(getResources().getString(R.string.please_input_right_phone_number));
                    }

                } else {
                    ToastUtils.showShort(getResources().getString(R.string.please_input_phone_number));

                }
            }
        });

        binding.loginEtRepwd.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                if(checkInput()) doForget();
                return true;
            }
            return false;
        });

        // 找回密码
        binding.commitBtn.setOnClickListener(v -> {
            if(checkInput()) doForget();
        });
    }

    private void doForget() {
        String userPhone = binding.loginEtUser.getText().toString().trim();
        String code = binding.loginEtCode.getText().toString().trim();
        String pwd = binding.loginEtPwd.getText().toString().trim();
        String repwd = binding.loginEtRepwd.getText().toString().trim();
        ForgetPasswordRequest request = new ForgetPasswordRequest();
        request.setPhone(userPhone);
        request.setVerificationCode(code);
        request.setNewPassword(pwd);
        request.setRepNewPassword(repwd);
        //配置请求对象结束
        RequestBody body = getRequestBody(request);
        api.doForgetPassword(body).enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        ToastUtils.showShort(response.body().getMsg());
                        mSwipeBackHelper.backwardAndFinish(LoginActivity.class);
                    } else {
                        ToastUtils.showShort(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<CommonResponse> call, Throwable t) {
                ToastUtils.showShort(getResources().getString(R.string.call_api_error));
            }
        });
    }

    private boolean checkInput() {
        String userPhone = binding.loginEtUser.getText().toString().trim();
        String code = binding.loginEtCode.getText().toString().trim();
        String newpwd = binding.loginEtPwd.getText().toString().trim();
        String repwd = binding.loginEtRepwd.getText().toString().trim();
        if (TextUtils.isEmpty(userPhone)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_phone_number));
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_code));
            return false;
        }
        if (TextUtils.isEmpty(newpwd)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_new_phone_pwd));
            return false;
        }
        if (TextUtils.isEmpty(repwd)) {
            ToastUtils.showShort(getResources().getString(R.string.please_reinput_new_phone_pwd));
            return false;
        }
        if (newpwd.length() < 6) {
            ToastUtils.showShort(getResources().getString(R.string.pwd_less_six));
            return false;
        }
        if (!newpwd.equals(repwd)) {
            ToastUtils.showShort(getResources().getString(R.string.different_pwd));
            return false;
        }
        if (!RegexUtils.isMobileExact(userPhone)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_right_phone_number));
            return false;
        }

        return true;
    }

    private void doVerifyCode() {
        String userPhone = binding.loginEtUser.getText().toString().trim();
        api.doGetVerifiCode(userPhone).enqueue(new Callback<GetVerifiCodeResponse>() {
            @Override
            public void onResponse(Call<GetVerifiCodeResponse> call, Response<GetVerifiCodeResponse> response) {
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        ToastUtils.showShort(response.body().getMsg());
                    } else {
                        ToastUtils.showShort(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<GetVerifiCodeResponse> call, Throwable t) {
                ToastUtils.showShort(ForgetActivity.this.getResources().getString(R.string.call_api_error));
            }
        });
    }

}
